package com.wht.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wht.annotation.RequiresPermissionsDesc;
import com.wht.domain.DtsRole;
import com.wht.service.DtsPermissionService;
import com.wht.service.DtsRoleService;
import com.wht.util.AdminResponseCode;
import com.wht.util.AdminResponseUtil;
import com.wht.util.AuthSupport;
import com.wht.util.ResponseUtil;
import com.wht.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @author wanghaotian
 * time:2022/12/08,0008 19:26
 * description:
 */

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    private static final Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @Autowired
    private DtsRoleService roleService;

    @Autowired
    private DtsPermissionService permissionService;

    @Autowired
    private ApplicationContext context;

    private List<PermVo> systemPermissions =null;
    private Set<String> systemPermissionsString = null;

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "角色查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order
                       ){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->角色管理->角色查询,请求参数,name:{},page:{}",name,page);
        IPage<DtsRole> iPage = roleService.querySelective(name,page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsRole> roleList = iPage.getRecords();
        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", roleList);
        logger.info("【请求结束】系统管理->角色管理->角色查询,响应结果:{}", JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }

    @GetMapping("/options")
    public Object options(){
        List<DtsRole> roleList = roleService.queryAll();
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->角色管理->查询所有角色");
        List<Map<String,Object>> options = new ArrayList<>(roleList.size());
        for (DtsRole role : roleList) {
            Map<String,Object> option = new HashMap<>(2);
            option.put("value", role.getId());
            option.put("label", role.getName());
            options.add(option);
        }
        logger.info("【请求结束】系统管理->角色管理->查询所有角色,响应结果:{}",JSONObject.toJSONString(options));
        return ResponseUtil.ok(options);
    }

    @RequiresPermissions("admin:role:read")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "角色详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->角色管理->角色详情,请求参数,id:{}",id);
        DtsRole role = roleService.findById(id);
        logger.info("【请求结束】系统管理->角色管理->角色详情,响应结果:{}",JSONObject.toJSONString(role));
        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->角色管理->角色添加,请求参数:{}",JSONObject.toJSONString(role));
        if(roleService.chechExist(role.getName())){
            logger.info("系统管理->角色管理->角色添加错误:{}", AdminResponseCode.ROLE_NAME_EXIST.desc());
            return AdminResponseUtil.fail(AdminResponseCode.ROLE_NAME_EXIST);
        }
        roleService.add(role);
        logger.info("【请求结束】系统管理->角色管理->角色添加,响应结果:{}",JSONObject.toJSONString(role));
        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->角色管理->角色编辑,请求参数:{}",JSONObject.toJSONString(role));
        roleService.updateById(role);
        logger.info("【请求结束】系统管理->角色管理->角色编辑,响应结果:{}", "成功!");
        return ResponseUtil.ok();
    }

}
